package com.xiaocz.hblsubstitutedriving.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaocz.common.Common;
import com.xiaocz.common.http.AsyncHttpPostFormData;
import com.xiaocz.common.http.AsyncHttpResponse;
import com.xiaocz.common.http.AsyncOkHttpClient;
import com.xiaocz.common.http.AsyncResponseHandler;
import com.xiaocz.common.model.RspModel;
import com.xiaocz.common.utils.AccountManagers;
import com.xiaocz.common.widgets.load.LoadFactory;
import com.xiaocz.common.widgets.load.core.LoadManager;
import com.xiaocz.common.widgets.load.view.AbsView;
import com.xiaocz.common.widgets.recycler.RecyclerViewAdapter;
import com.xiaocz.common.widgets.recycler.decoration.DividerItemDecoration;
import com.xiaocz.hblsubstitutedriving.App;
import com.xiaocz.hblsubstitutedriving.R;
import com.xiaocz.hblsubstitutedriving.adapter.ExpenseStandardListAdapter;
import com.xiaocz.hblsubstitutedriving.base.BaseActivity;
import com.xiaocz.hblsubstitutedriving.model.ExpenseStandard;
import com.xiaocz.hblsubstitutedriving.model.OrderPrice;
import com.xiaocz.hblsubstitutedriving.utils.Constants;
import com.xiaocz.hblsubstitutedriving.widget.EmptyView;
import com.xiaocz.hblsubstitutedriving.widget.ErrorView;
import com.xiaocz.hblsubstitutedriving.widget.LoadingView;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.BindView;


/**
 * 计价标准
 */
public class ExpenseStandardActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    public RecyclerView mRecyclerView;
    @BindView(R.id.tv_wait_time)
    TextView tvWaitTime;
    @BindView(R.id.layout_subsidy)
    LinearLayout layoutSubsidy;
    @BindView(R.id.tv_subsidy)
    TextView tvSubsidy;

    /**
     * DATA
     */
    private LoadManager mLoadManager;
    private ExpenseStandardListAdapter expenseStandardListAdapter;

    public static void show(Context context) {
        Intent intent = new Intent(context, ExpenseStandardActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_expense_standard;
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
        mLoadManager = LoadFactory.getInstance().register(refreshLayout, new AbsView.OnReloadListener() {
            @Override
            public void onReload(View view) {
                mLoadManager.showStateView(LoadingView.class);
                setParam();
            }
        });
        setRefresh();
    }

    private void setRefresh() {
        // 下拉刷新
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh();
                setParam();
            }
        });
        setParam();
    }

    @Override
    protected void initData() {
        super.initData();
        mTitle.setText("代驾计费标准");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext,
                DividerItemDecoration.VERTICAL, R.drawable.shape_divider_line, 6, true));
        expenseStandardListAdapter = new ExpenseStandardListAdapter(new RecyclerViewAdapter.AdapterListenerImpl<OrderPrice>() {
            @Override
            public void onItemClick(RecyclerViewAdapter.ViewHolder<OrderPrice> holder, OrderPrice data) {
                super.onItemClick(holder, data);
            }
        });
        mRecyclerView.setAdapter(expenseStandardListAdapter);
    }

    private void setParam() {
        if (Constants.getLocation() != null) {
            webExpensestandard(Constants.getLocation().getProvince(), Constants.getLocation().getCity());
        }else {
            mLoadManager.showSuccessView();
            App.getInstance().showMessage(getResources().getString(R.string.none_location));
        }
    }

    /**
     * 计价标准
     */
    private void webExpensestandard(String province_name, final String city_name) {
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("driver_id", AccountManagers.getUserId());
        asyncHttpPostFormData.addFormData("province_name", province_name);
        asyncHttpPostFormData.addFormData("city_name", city_name);
        okHttpClient.post(Common.Constant.API_IP + "api/Common/getCityPrice", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                mLoadManager.showStateView(ErrorView.class);
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                Type type = new TypeToken<RspModel<ExpenseStandard>>() {
                }.getType();
                RspModel<ExpenseStandard> rspModel = getAnalysisBase(mLoadManager, response, type, "计价标准");
                if (rspModel == null) {
                    mLoadManager.showStateView(EmptyView.class);
                    return;
                }
                switch (rspModel.getCode()) {
                    case 200:
                        if (rspModel.getData() != null && !"[]".equals(rspModel.getData().getList().toString())) {
                            tvRight.setText(city_name);
                            expenseStandardListAdapter.replace(rspModel.getData().getList());
                            tvWaitTime.setText(String.valueOf("等候" + rspModel.getData().getFreewaittime() + "分钟内免费，超出每分钟加收" + rspModel.getData().getWaitprice() + "元"));
                            if(!TextUtils.isEmpty(rspModel.getData().getTempname())){
                                layoutSubsidy.setVisibility(View.VISIBLE);
                                tvSubsidy.setText(String.valueOf(rspModel.getData().getTempname()+",每单加收"+rspModel.getData().getTempprice()+"元"));
                            }
                        } else {
                            mLoadManager.showStateView(EmptyView.class);
                        }
                        break;
                    default:
                        mLoadManager.showStateView(ErrorView.class);
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }
}
